import React, { useState } from 'react';
import NumberField from '../../../../components/common/NumberField';
import StatefulCalculationCard from '../../../../components/common/StatefulCalculationCard';
import ExportData from '../../../../components/common/ExportCalculateData';
import type { ResultItem } from '../../../../../shared/types';

// 定义冲击力计算表单数据类型
export interface ImpactFormValues {
  mr: number;
  Hr: number;
  Tb: number;
  g: number;
}

// 定义冲击荷载强度计算表单数据类型
export interface StrengthFormValues {
  Fr: number;
  Dr: number;
  beta: number;
  Tb: number;
}

// 冲击力计算初始值配置
const impactInitialValues: ImpactFormValues = {
  mr: 10.0,
  Hr: 5.0,
  Tb: 0.5,
  g: 9.8,
};

// 冲击荷载强度计算初始值配置
const strengthInitialValues: StrengthFormValues = {
  Fr: 1.0,
  Dr: 1.0,
  beta: 30.0,
  Tb: 0.5,
};

// 冲击力计算卡片组件配置
const ImpactLoadConfig = {
  key: 'ImpactLoadCalculation',
  label: '冲击力计算',
  formKey: 'rock-impact-load',
  initialValues: impactInitialValues,
  calculationType: 'rock-impact-load',
  resultFormatter: (data: any) => [
    { key: 'v0', name: '落石速度 v₀', value: data.v0, unit: 'm/s' },
    { key: 't', name: '冲击时间 t', value: data.t, unit: 's' },
    { key: 'Fr', name: '冲击荷载 Fᵣ', value: data.Fr, unit: 'kN' },
  ],
  renderFormFields: () => (
    <>
      <NumberField name="mr" label="落石质量 mr (kg)" required min={0} step={0.1} tooltip="落石的质量" />
      <NumberField name="Hr" label="下落高度 Hr (m)" required min={0} step={0.1} tooltip="落石从释放点到冲击点的垂直距离" />
      <NumberField name="Tb" label="缓冲层厚度 Tb (m)" required min={0} step={0.1} tooltip="结构表面的缓冲材料厚度" />
      <NumberField name="g" label="重力加速度 g (m/s²)" required min={0} step={0.1} tooltip="重力加速度，默认值为9.8m/s²" />
    </>
  ),
};

// 冲击荷载强度计算卡片组件配置
const ImpactStrengthConfig = {
  key: 'ImpactStrengthCalculation',
  label: '冲击荷载强度计算',
  formKey: 'rock-impact-strength',
  initialValues: strengthInitialValues,
  calculationType: 'rock-impact-strength',
  resultFormatter: (data: any) => [
    { key: 'pr', name: '冲击荷载强度 pᵣ', value: data.rock_impact_strength, unit: 'kN/m²' },
  ],
  renderFormFields: () => (
    <>
      <NumberField name="Fr" label="冲击荷载 Fr (kN)" required min={0} step={0.0001} tooltip="冲击荷载值" />
      <NumberField name="Dr" label="落石直径 Dr (m)" required min={0} step={0.001} tooltip="落石的等效直径" />
      <NumberField name="beta" label="冲击荷载扩散角 β (°)" required min={0} step={0.1} tooltip="荷载在缓冲层中扩散的角度" />
      <NumberField name="Tb" label="缓冲层厚度 Tb (m)" required min={0} step={0.001} tooltip="结构表面的缓冲材料厚度" />
    </>
  ),
};

// 冲击力计算卡片组件实现
export const ImpactLoadCard: React.FC = () => {
  const [lastResults, setLastResults] = useState<ResultItem[] | null>(null);
  
  return (
    <StatefulCalculationCard
      title={ImpactLoadConfig.label}
      formKey={ImpactLoadConfig.formKey}
      initialValues={ImpactLoadConfig.initialValues}
      renderFormFields={ImpactLoadConfig.renderFormFields}
      calculationType={ImpactLoadConfig.calculationType}
      resultFormatter={(data) => {
        const results = ImpactLoadConfig.resultFormatter(data);
        setLastResults(results);
        return results;
      }}
      columnCount={1}
      tableHeight={300}
      exportComponent={(form) => (
        <ExportData
          siteName={ImpactLoadConfig.label}
          inputs={form.getFieldsValue()}
          results={lastResults ? Object.fromEntries(lastResults.map(r => [r.key, r.value])) : {}}
          calculationType={ImpactLoadConfig.calculationType}
        />
      )}
    />
  );
};

// 冲击荷载强度计算卡片组件实现
export const ImpactStrengthCard: React.FC = () => {
  const [lastResults, setLastResults] = useState<ResultItem[] | null>(null);
  
  return (
    <StatefulCalculationCard
      title={ImpactStrengthConfig.label}
      formKey={ImpactStrengthConfig.formKey}
      initialValues={ImpactStrengthConfig.initialValues}
      renderFormFields={ImpactStrengthConfig.renderFormFields}
      calculationType={ImpactStrengthConfig.calculationType}
      resultFormatter={(data) => {
        const results = ImpactStrengthConfig.resultFormatter(data);
        setLastResults(results);
        return results;
      }}
      columnCount={1}
      tableHeight={300}
      exportComponent={(form) => (
        <ExportData
          siteName={ImpactStrengthConfig.label}
          inputs={form.getFieldsValue()}
          results={lastResults ? Object.fromEntries(lastResults.map(r => [r.key, r.value])) : {}}
          calculationType={ImpactStrengthConfig.calculationType}
        />
      )}
    />
  );
};